<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/23 0023
 * Time: 11:07
 * 黄河
 */

namespace app\api\controller;

use app\admin\model\Area;
use app\admin\model\Baners;
use app\admin\model\DismantlementSchedule;
use app\admin\model\Navigation;
use app\admin\model\Article;
use app\admin\model\Partynews;
use app\admin\model\Policy;
use app\admin\model\Notice;
use think\Request;

/**
 * Class HomeFrame
 * @package app\api\controller
 * 获取首页模板，首页为自定义页面
 */
class HomeFrame extends BaseController
{
    /**
     * 首页接口数据
     * @DateTime 2018-06-11
     */
    public function getHomeFrame()
    {
        $where1 = [
            'site' => 1,
            'status' => 1
        ];
        $where2 = [
            'status' => 1,
            'type' => 1
        ];
        $where3 = [
            'status' => 1
        ];

        $Baners = Baners::where($where1)->order('sort asc')->limit(0,10)->select();
        $Navigation = Navigation::where($where2)->order('sort asc')->select();

        $fields = 'a.id,a.title,a.imgUrl,a.createDate,c.name';
        $Policy = Policy::alias('a')->join('category c','a.categoryId = c.id')->field($fields)->order('createDate DESC')
            ->limit(0, 3)->select();

        // 党建要闻
        //$Partynews = Partynews::where($where3)->order('createDate DESC')->limit(0,3)->select();
        // 城建要闻
        $citynews = Article::where(['type' => 'citynews'])->order('sorting ASC,createDate DESC')->limit(0, 10)->select();

        $fields = "a.id,a.title,a.content,a.createDate,d.name";
        $notice = Notice::alias('a')->join('department d','a.departmentId = d.id')->field($fields)->order('createDate DESC')->limit(0, 3)->select();

        $data['baners'] = $Baners;
        $data['menu'] = $Navigation;
        $data['filesnotice'] = $Policy;
        //$data['partynews'] = $Partynews;
        $data['citynews'] = $citynews;
        $data['notice'] = $notice;
        $data['announcement'] = null;
        $data['work_dynamic'] = null;

        return show(config('status.SUCCESS_STATUS'),'ok',$data);
    }

    public function organization(){
        $Company = \app\admin\model\Company::get(1);
        if($Company){
            $Company['cityName'] = Area::where('id', $Company['city'])->value('name');
            $Company['areaName'] = Area::where('id', $Company['area'])->value('name');
            $Company['townName'] = Area::where('id', $Company['town'])->value('name');

            return show(config('status.SUCCESS_STATUS'),'ok',$Company);
        }else{
            return show(config('status.ERROR_STATUS'),'暂无数据',null);
        }
    }

    public function department(){
        $Department = \app\admin\model\Department::where('status',1)->order('sorting ASC')->select();
        if($Department){
            return show(config('status.SUCCESS_STATUS'),'ok',$Department);
        }else{
            return show(config('status.ERROR_STATUS'),'暂无数据',null);
        }
    }

    public function departmentDetail(Request $request){
        $param = $request->param();
        if(empty($param['id'])){
            return show(config('status.ERROR_STATUS'),'参数错误','参数错误');
        }
        $Department = \app\admin\model\Department::get($param['id']);
        if($Department){
            return show(config('status.SUCCESS_STATUS'),'ok',$Department);
        }else{
            return show(config('status.ERROR_STATUS'),'暂无数据',null);
        }
    }

    //拆迁上报列表
    public function getremoveList(){

        $Dismantlement = \app\admin\model\Dismantlement::where(true)->select();
        if($Dismantlement){
            foreach ($Dismantlement as &$v){
                $Image = \app\admin\model\Image::where('relatedId',$v['id'])->field('imgUrl')->select();

                if($Image){
                    $list = [];
                    foreach ($Image as $val){
                        array_push($list,$val['imgUrl']);
                    }
                    $v['img'] = $list[0];
                }else{
                    $v['img'] = NULL;
                }
            }

            return show(config('status.SUCCESS_STATUS'),'ok',$Dismantlement);
        }else{
            return show(config('status.ERROR_STATUS'),'暂无数据',null);
        }

    }

    public function removeDetail(Request $request){
        $param = $request->param();
        if(empty($param['id'])){
            return show(config('status.ERROR_STATUS'),'参数错误','参数错误');
        }
        $Dismantlement = \app\admin\model\Dismantlement::get($param['id']);
        if($Dismantlement){
            $Image = \app\admin\model\Image::where('relatedId',$param['id'])->field('imgUrl')->select();
            if($Image){
                $Dismantlement['imglist'] = $Image;
            }else{
                $Dismantlement['imglist'] = [];
            }
            $DismantlementSchedule = DismantlementSchedule::where('disId',$param['id'])->select();
            if($DismantlementSchedule){
                $Dismantlement['scheduleList'] = $DismantlementSchedule;
            }else{
                $Dismantlement['scheduleList'] = [];
            }
            return show(config('status.SUCCESS_STATUS'),'ok',$Dismantlement);
        }else{
            return show(config('status.ERROR_STATUS'),'暂无数据',null);
        }
    }

}